package com.flymobi.mshop.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.flymobi.mshop.R;

public class Main extends ActivityGroup {
	private TabHost tabHost;
	
	public TabHost getTabHost() {
		return tabHost;
	}

	public void setTabHost(TabHost tabHost) {
		this.tabHost = tabHost;
	}

	private LayoutInflater inflater;
	private View home, shopCar, myCenter, more, classes;
	private String lastTab="home";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(getLocalActivityManager());
		inflater = LayoutInflater.from(this);
		
		home = inflater.inflate(R.layout.tab_1, null);
		classes = inflater.inflate(R.layout.tab_2, null);
		shopCar = inflater.inflate(R.layout.tab_3, null);
		myCenter = inflater.inflate(R.layout.tab_4, null);
		more = inflater.inflate(R.layout.tab_5, null);

		tabHost.addTab(tabHost.newTabSpec("home").setIndicator(home)
				.setContent(new Intent(this, Home.class)));
		tabHost.addTab(tabHost.newTabSpec("classes").setIndicator(classes)
				.setContent(new Intent(this, Classes.class)));
		tabHost.addTab(tabHost.newTabSpec("shopCar").setIndicator(shopCar)
				.setContent(new Intent(this, ShopCar.class)));
		
		tabHost.addTab(tabHost.newTabSpec("myCenter").setIndicator(myCenter)
				.setContent(new Intent(this, MyCenter.class)));
		tabHost.addTab(tabHost.newTabSpec("more").setIndicator(more)
				.setContent(new Intent(this, More.class)));
		tabHost.setSelected(false);
		//tabHost.setCurrentTabByTag("home");
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				Log.d("tab:", tabId);
				
				if (!Common.isLogin()&&tabId.equals("myCenter")) {
					tabHost.setCurrentTabByTag(lastTab);
					Intent intent = new Intent(Main.this, Login.class);
					startActivity(intent);
				}else{
					lastTab=tabId;
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		//选择tab
		String action=(String)Common.context.get("action");
		if(action==null){
			action="0";
		}
		tabHost.setCurrentTab(Integer.parseInt(action));
		Common.context.remove("action");
		//选择tab
	}
}
